﻿namespace com.oakwoodsys.ConsultantCare
{
    using System.Collections.Generic;

    using com.oakwoodsys.ConsultantCareDao;

    using Microsoft.SharePoint;

    /// <summary>
    /// List Item Events
    /// </summary>
    public class ConsultantCareActivityItemEventReceiver : SPItemEventReceiver
    {
        #region Methods

        /// <summary>
        /// An item was added.
        /// </summary>
        public override void ItemAdded( SPItemEventProperties properties )
        {
            if ( properties.ListItem.ContentType.Name.Equals( ConsultantCareActivity.GetContentTypeName<ConsultantCareActivity>( ) ) )
            {
                ConsultantCareActivity activity = getActivity( properties );
                if ( activity != null && activity.Active.HasValue && activity.Active.Value )
                {
                    List<ConsultantCareActivity> activities = new List<ConsultantCareActivity>( );
                    activities.Add( activity );
                    List<Consultant> consultants = getConsultants( properties, activity );
                    CalendarEventCreator creator = new CalendarEventCreator( properties.WebUrl );
                    creator.AddCalendarEvents( consultants, activities );
                }
            }
        }

        /// <summary>
        /// An item is being deleted.
        /// </summary>
        /// <param name="properties"></param>
        public override void ItemDeleting( SPItemEventProperties properties )
        {
            DeletionDao dao = new DeletionDao( properties.WebUrl );
            dao.Add( "A", properties.ListItem.UniqueId );

            //CalendarEventCreator creator = new CalendarEventCreator( properties.WebUrl );
            //creator.RemoveCalendarEventsByActivity( properties.ListItem.UniqueId );
        }

        /// <summary>
        /// An item was updated.
        /// Remove all calendar events associated with the acvtivity. If the activity is currently active, then add new calendar events for applicable consultants with the activity.
        /// </summary>
        /// <param name="properties"></param>
        public override void ItemUpdated( SPItemEventProperties properties )
        {
            if ( properties.ListItem.ContentType.Name.Equals( ConsultantCareActivity.GetContentTypeName<ConsultantCareActivity>( ) ) )
            {
                ConsultantCareActivity activity = getActivity( properties );
                // if valid activity
                if ( activity != null )
                {
                    // remove calendar events associated with activity
                    CalendarEventCreator creator = new CalendarEventCreator( properties.WebUrl );
                    creator.RemoveCalendarEvents( activity );

                    // if activity is active
                    if ( activity.Active.HasValue && activity.Active.Value )
                    {
                        List<ConsultantCareActivity> activities = new List<ConsultantCareActivity>( );
                        activities.Add( activity );
                        List<Consultant> consultants = getConsultants( properties, activity );
                        // add calendar events
                        creator.AddCalendarEvents( consultants, activities );
                    }
                }
            }
        }

        ConsultantCareActivity getActivity( SPItemEventProperties properties )
        {
            ConsultantCareActivityDao dao = new ConsultantCareActivityDao( properties.WebUrl );
            return dao.Get( properties.ListItem.UniqueId );
        }

        List<ConsultantActivityID> getConsultantRelationship( SPItemEventProperties properties, ConsultantCareActivity activity )
        {
            ConsultantActivityIDDao dao = new ConsultantActivityIDDao( properties.WebUrl );
            return dao.GetByActivity( activity.UniqueId );
        }

        List<Consultant> getConsultants( SPItemEventProperties properties, ConsultantCareActivity activity )
        {
            if ( activity.Associated.HasValue )
            {
                ConsultantDao dao = new ConsultantDao( properties.WebUrl );
                switch ( activity.Associated.Value )
                {
                    case Associated.Both:
                        return dao.GetAllActive( );
                    case Associated.Consultant:
                        return dao.GetAllActiveConsultant( );
                    case Associated.Permanent:
                        return dao.GetAllActivePermanent( );
                    case Associated.None:
                    case Associated.Invalid:
                    default:
                        return new List<Consultant>( );
                }
            }
            else
                return new List<Consultant>( );
        }

        #endregion Methods
    }
}